package action;

import Impl.MessageDaoImpl;
import entity.Message;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AddMessage")
public class AddMessage extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("-----------正在执行AddMessage-------------");
        MessageDaoImpl messageDao = new MessageDaoImpl();
        String receiver = request.getParameter("receiver");
        String content = request.getParameter("message");
        String sender = (String) request.getSession().getAttribute("uname");
        Message message = new Message(receiver,content,sender,0);
        response.setContentType("utf-8");
        response.setCharacterEncoding("utf-8");
        if (receiver != null && !receiver.equals("") && content != null && !content.equals("")) {
            messageDao.add(message);
            response.getWriter().write("<span style='color:green'>发送成功！</span>");
            System.out.println("发送成功");
        } else {
            if (receiver == null || receiver.equals("")) {
                response.getWriter().write("<span style='color:red'>请输入收件人</span>");
                System.out.println("请输入收件人");
            } else {
                response.getWriter().write("<span style='color:red'>请输入需要发送的信息</span>");
                System.out.println("请输入需要发送的信息");
            }
            System.out.println("发送失败");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
